{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "perceptron.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyM7wa27z9Dzko5BB8s6dmyG",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/SummerLife/EmbeddedSystem/blob/master/MachineLearning/gist/perceptron.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sAkkLseJOMZ2",
        "colab_type": "text"
      },
      "source": [
        "![image.png]()\n",
        "\n",
        "\n",
        "\n",
        "\\begin{eqnarray}\n",
        "y & = & \\left\\{\\begin{array}{ll}\n",
        "0 & \\left(w_{1} x_{1}+w_{2} x_{2} \\leqslant \\theta\\right) \\\\\n",
        "1 & \\left(w_{1} x_{1}+w_{2} x_{2}>\\theta\\right)\n",
        "\\end{array}\\right.\n",
        "\\end{eqnarray}"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "idpoL4JSNnj7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def AND(x1, x2):\n",
        "    w1, w2, theta = 0.5, 0.5, 0.7\n",
        "    tmp = x1*w1 + x2*w2\n",
        "    if tmp <= theta:\n",
        "        return 0\n",
        "    elif tmp > theta:\n",
        "        return 1"
      ],
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6ZYE73zQROiV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1a3c7fd7-d54d-480a-8b9f-a5e0c76d0875"
      },
      "source": [
        "AND(0,0)"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UYtYb5ExRUck",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d4fc19db-3043-4c7f-f4f3-42c6ea58be7f"
      },
      "source": [
        "AND(1,0)"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xxyn41CTRWfP",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1c29f0db-f28f-419e-d9b5-27f34180d0d5"
      },
      "source": [
        "AND(0,1)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GP9i7vKORXSj",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a767231e-c29a-44a4-d371-935e1df0fb04"
      },
      "source": [
        "AND(1,1)"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sdsRypp-R8Bt",
        "colab_type": "text"
      },
      "source": [
        "\\begin{eqnarray}\n",
        "y & = & \\left\\{\\begin{array}{ll}\n",
        "0 & \\left(b+w_{1} x_{1}+w_{2} x_{2} \\leqslant 0\\right) \\\\\n",
        "1 & \\left(b+w_{1} x_{1}+w_{2} x_{2}>0\\right)\n",
        "\\end{array}\\right.\n",
        "\\end{eqnarray}"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "By-DQ_3IRYaW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jf3bhXyrSVkU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = np.array([0, 1])"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IkcJiFrySY7j",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "w = np.array([0.5, 0.5])"
      ],
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-l38GgiGSdDw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "b = -0.7"
      ],
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zahCoap5SfXq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5fd1ec04-1f93-48ca-e5bd-3f3142d65acc"
      },
      "source": [
        "w * x"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0. , 0.5])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YfuMGPimSiOW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "f68c10d1-eccd-41b5-9b20-b14ce2d984b1"
      },
      "source": [
        "np.sum(w * x)"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.5"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K-q1vLxSSpQd",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "42ca3ac0-0fbb-4431-eca5-dfd81d22e5b7"
      },
      "source": [
        "np.sum(w * x) + b"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-0.19999999999999996"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QdpQrrAASsOY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def AND(x1, x2):\n",
        "    x = np.array([x1, x2])\n",
        "    w = np.array([0.5, 0.5])\n",
        "    b = - 0.7\n",
        "    tmp = np.sum(x * w) + b\n",
        "    if tmp <= 0:\n",
        "        return 0\n",
        "    else:\n",
        "        return 1"
      ],
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uUExJITZCsZk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def NAND(x1, x2):\n",
        "    x = np.array([x1, x2])\n",
        "    w = np.array([-0.5, -0.5])\n",
        "    b = 0.7\n",
        "    tmp = np.sum(x * w) + b\n",
        "    if tmp <= 0:\n",
        "        return 0\n",
        "    else:\n",
        "        return 1"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P9wPc30-FYpx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "f40883db-ac75-4b44-b663-52b67fdf9a50"
      },
      "source": [
        "NAND(0,0)"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K4M3QybDFbQf",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "8b5dbb9d-a08a-4a1e-91b2-90ec072ce957"
      },
      "source": [
        "NAND(1,0)"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "90kOnZnaFf8s",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "820e370d-13a5-48fc-f448-5c1d0d193973"
      },
      "source": [
        "NAND(1,1)"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4hEwFzPYFidH",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "7e07505e-160b-46b7-cd0f-b0792f16ed95"
      },
      "source": [
        "NAND(0,1)"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S7sqK9JTF60d",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def OR(x1, x2):\n",
        "    x = np.array([x1, x2])\n",
        "    w = np.array([0.5,0.5])\n",
        "    b = -0.2\n",
        "    tmp = np.sum(x * w) + b\n",
        "    if tmp <= 0:\n",
        "        return 0\n",
        "    else:\n",
        "        return 1"
      ],
      "execution_count": 14,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mSwpCne7F-vV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "50bcfddf-723c-43e8-9f26-a6d54eb12138"
      },
      "source": [
        "OR(0,0)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WxlVKsTUGYpF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "573596b0-9b23-448e-d79f-85ee2581b7fc"
      },
      "source": [
        "OR(1,1)"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RoGP5oYjGa05",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "6cc85953-6830-4976-832f-5ce90715a67b"
      },
      "source": [
        "OR(1,0)"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KBMDxthPHZhG",
        "colab_type": "text"
      },
      "source": [
        "### 异或门的输出\n",
        "\n",
        "![image.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hyuwY4IhHtfJ",
        "colab_type": "text"
      },
      "source": [
        "![image.png]()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4_1nXyrcMYhp",
        "colab_type": "text"
      },
      "source": [
        "### multi-layered perceptron\n",
        "\n",
        "![image.png]()\n",
        "\n",
        "![image.png]()"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "r1POcwhsGcmB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def XOR(x1, x2):\n",
        "    s1 = NAND(x1, x2)\n",
        "    s2 = OR(x1, x2)\n",
        "    y = AND(s1, s2)\n",
        "    return y"
      ],
      "execution_count": 20,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HfBlRLD4M4qz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4e87c2b7-8263-4e25-b316-867ea4595e46"
      },
      "source": [
        "XOR(0,0)"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Qh1cinlGNTxu",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "28e98167-fb37-44f6-aba3-b47bece33f03"
      },
      "source": [
        "XOR(1,0)"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FKZkWR4LNVfi",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1ba66911-de24-4f2b-c400-51bc62a9b44b"
      },
      "source": [
        "XOR(0,1)"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6O2pzv2UNX0v",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5149c2ec-0e10-4a5e-a61c-c22e6d949bf5"
      },
      "source": [
        "XOR(1,1)"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hJpnM5BvOOfg",
        "colab_type": "text"
      },
      "source": [
        "![image.png]()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cDG9Lw9oObL5",
        "colab_type": "text"
      },
      "source": [
        "在上图所示的 2 层感知机中，先在第 0 层和第 1 层的神经元之间进行 信号的传送和接收，然后在第 1 层和第 2 层之间进行信号的传送和接收，具 体如下所示。\n",
        "\n",
        "1. 第 0 层的两个神经元接收输入信号，并将信号发送至第 1 层的神经元。 \n",
        "\n",
        "2. 第 1 层的神经元将信号发送至第 2 层的神经元，第 2 层的神经元输出 y。\n"
      ]
    }
  ]
}